<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Config
 *
 * @property int $id
 * @property string $module 应用模块
 * @property string|null $name 变量名
 * @property string|null $group 分组
 * @property string|null $title 变量标题
 * @property string|null $tip 变量描述
 * @property string|null $type 类型
 * @property string|null $value 变量值
 * @property string|null $content 变量字典数据
 * @property string|null $rule 验证规则
 * @property string|null $extend 扩展属性
 * @property string|null $setting 配置
 * @property int $required 是否必填，0=否，1=是
 * @method static \Illuminate\Database\Eloquent\Builder|Config newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Config newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Config query()
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereExtend($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereGroup($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereModule($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereRequired($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereRule($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereSetting($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereTip($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Config whereValue($value)
 * @mixin \Eloquent
 */
class Config extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'config';
    public $timestamps = false;


    /**
     * 类型转换
     *
     * @var array
     */
    protected $casts = [
//        'content' => 'array',
    ];

    public function getContentAttribute($value){
//        dump($value&&is_string($value)?json_decode($value,true):$value);
        return $value&&is_string($value)?json_decode($value,true):"";
    }

    public function setContentAttribute($value){
        $this->attributes['content'] = is_string($value)?$value:json_encode($value,JSON_UNESCAPED_UNICODE);
    }


    /**
     * 读取配置类型
     * @return array
     */
    public static function getTypeList(): array
    {
        return [
            'text'          => '字符串',
            'textarea'      => '长文本',
            'editor'        => '编辑器',
            'number'        => '数字',
            'hidden'        => '隐藏字段',
            'switch'        => '开关',
            'radio'         => '单选',
            'checkbox'      => '多选',
            'select'        => '下拉单选',
            'load'          => '下拉选框联动',
            'loads'         => '联动多个字段',
            'multipleSelect'=> '下拉多选',
            'selectTable'   => '表格选择器',
            'listbox'       => '多选盒子',
            'map'           => '地图',
            'slider'        => '滑动条',
            'email'         => '邮箱',
            'password'      => '密码',
            'url'           => '链接',
            'ip'            => 'IP地址',
            'mobile'        => '手机号',
            'color'         => '颜色选择器',
            'date'          => '日期',
            'time'          => '时间',
            'datetime'      => '日期时间',
            'dateRange'     => '日期范围',
            'timeRange'     => '时间范围',
            'datetimeRange' => '日期时间范围',
            'range'         => '范围',
            'image'         => '图片上传',
            'multipleImage' => '多图上传',
            'file'          => '文件上传',
            'multipleFile'  => '多文件上传',
            'markdown'      => 'markdown编辑器',
            'display'       => '仅显示字段',
            'currency'      => '单位符号',
            'rate'          => '费率',
            'tags'          => '标签',
            'icon'          => '图标选择器',
            'tree'          => '树形选择器',
            'divider'       => '分割线',
            'keyValue'      => '键值组',
            'embeds'        => '固定键值对象',
            'list'          => '一维数组',
            'table'         => '二维数组（table）',
            'array'         => '二维数组（array）',
            'html'          => '自定义Html',
        ];
    }

    public static function getRegexList()
    {
        $regexList = [
            'required' => '必选',
            'digits'   => '数字',
            'letters'  => '字母',
            'date'     => '日期',
            'time'     => '时间',
            'email'    => '邮箱',
            'url'      => '网址',
            'qq'       => 'QQ号',
            'IDcard'   => '身份证',
            'tel'      => '座机电话',
            'mobile'   => '手机号',
            'zipcode'  => '邮编',
            'chinese'  => '中文',
            'username' => '用户名',
            'password' => '密码'
        ];
        return $regexList;
    }

    /**
     * 读取分类分组列表
     * @return array
     */
    public static function getGroupList(): array
    {
//        'wechat'=>'公众号配置','upload'=>'上传配置','min_program'=>'小程序配置','test'=>'测试配置'
        return ['basic' => '基础配置'];
    }

    /**
     * @param string|array $name
     * @return mixed
     */
    public static function getValues(string|array $name = ""): mixed
    {
       $data = self::query()->when($name, function ($query,$value){
           if (is_array($value)){
               $query->whereIn("name",$value);
           }else{
               $query->where("name",$value);
           }
       })->pluck('value', 'name');
       if ($name&&is_string($name)){
           return $data[$name];
       }else{
           return $data;
       }
    }

}
